package top.lshaci.learning.springboot.mybatisplus.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.lshaci.learning.springboot.mybatisplus.entity.SimpleSn;
import top.lshaci.learning.springboot.mybatisplus.mapper.SimpleSnMapper;
import top.lshaci.learning.springboot.mybatisplus.service.SimpleSnService;

import java.util.Optional;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lshaci
 * @since 2023-05-31
 */
@Service
public class SimpleSnServiceImpl extends ServiceImpl<SimpleSnMapper, SimpleSn> implements SimpleSnService {

    ReentrantLock lock = new ReentrantLock();

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String nextSnSynchronizedStr(String tag, String prefix) {
        String lock = StrUtil.format("LOCK_SIMPLE_SN_{}_{}", tag, prefix).intern();
        synchronized (lock) {
            LambdaQueryWrapper<SimpleSn> query = Wrappers.lambdaQuery(SimpleSn.class)
                    .eq(SimpleSn::getTag, tag)
                    .eq(SimpleSn::getPrefix, prefix)
                    .last(" for update");
            SimpleSn simpleSn = Optional.ofNullable(baseMapper.selectOne(query)).orElseGet(() -> new SimpleSn(tag, prefix));
            String next = simpleSn.next();
            saveOrUpdate(simpleSn);
            return next;
        }
    }

    @Override
    public synchronized String nextSnSynchronizedMethod(String tag, String prefix) {
        LambdaQueryWrapper<SimpleSn> query = Wrappers.lambdaQuery(SimpleSn.class)
                .eq(SimpleSn::getTag, tag)
                .eq(SimpleSn::getPrefix, prefix);
        SimpleSn simpleSn = Optional.ofNullable(baseMapper.selectOne(query)).orElseGet(() -> new SimpleSn(tag, prefix));
        System.err.println(Thread.currentThread().getName() + ": 保存前查询：" + simpleSn.getVal());
        String next = simpleSn.next();
        saveOrUpdate(simpleSn);
        SimpleSn dbSimpleSn = baseMapper.selectOne(query);
        System.err.println(Thread.currentThread().getName() + ": 保存后查询：" + dbSimpleSn.getVal());
        return next;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String nextSnLock(String tag, String prefix) {
        lock.lock();
        try {

            LambdaQueryWrapper<SimpleSn> query = Wrappers.lambdaQuery(SimpleSn.class)
                    .eq(SimpleSn::getTag, tag)
                    .eq(SimpleSn::getPrefix, prefix);
            SimpleSn simpleSn = Optional.ofNullable(baseMapper.selectOne(query)).orElseGet(() -> new SimpleSn(tag, prefix));
            String next = simpleSn.next();
            saveOrUpdate(simpleSn);
            return next;
        } finally {
            lock.unlock();
        }
    }
}
